package com.ysx.user.controller;


import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysx.common.pojo.Result;
import com.ysx.common.pojo.StatusCode;
import com.ysx.common.utils.RequestHeaderInfoUtils;
import com.ysx.core.controller.AbstractCoreController;
import com.ysx.exception.ToutiaoException;
import com.ysx.user.dto.UserRelationDto;
import com.ysx.user.pojo.ApUserFollow;
import com.ysx.user.service.ApUserFollowService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
* <p>
* APP用户关注信息表 控制器</p>
* @author ljh
* @since 2021-09-08
*/
@Api(value="APP用户关注信息表",tags = "ApUserFollowController")
@RestController
@RequestMapping("/apUserFollow")
public class ApUserFollowController extends AbstractCoreController<ApUserFollow> {

    private ApUserFollowService apUserFollowService;

    //注入
    @Autowired
    public ApUserFollowController(ApUserFollowService apUserFollowService) {
        super(apUserFollowService);
        this.apUserFollowService=apUserFollowService;
    }

    //当前登录用户关注某一个作者 或者取消某一个作者
    @PostMapping("/follow")
    public Result follow(@RequestBody UserRelationDto relationDto) throws Exception {
        //1.先获取当前登录的用户 判断是否为0 如果为 0 则 为匿名用户 不能关注
        String userInfo = RequestHeaderInfoUtils.getHeaderUserId();
        if (StringUtils.isEmpty(userInfo)) {
            //需要登录
            throw new ToutiaoException(StatusCode.NEED_LOGIN.code(), StatusCode.NEED_LOGIN.message());
        }
        //如果是0 则是匿名用户 也不能关注或者取消关注
        if (RequestHeaderInfoUtils.isAnonymous()) {
            throw new ToutiaoException(StatusCode.NEED_LOGIN.code(), StatusCode.NEED_LOGIN.message());
        }
        if(relationDto==null){
            throw new ToutiaoException(StatusCode.CUSTOM_FAILURE.code(), "传递的参数有问题");
        }
        //2.能获取到登录用户 则 开始执行业务
        Integer currentUserId = Integer.valueOf(userInfo);
        apUserFollowService.followUserByWho(relationDto, currentUserId);
        return Result.ok();
    }

    //查询用户关注状态信息
    @GetMapping("/getApUserFollow")
    ApUserFollow getApUserFollow(@RequestParam(name="followId")Integer followId,@RequestParam(name="userId")Integer userId){

        QueryWrapper<ApUserFollow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("follow_id",followId);
        queryWrapper.eq("user_id",userId);
        return apUserFollowService.getOne(queryWrapper);
    }

}

